package com.hall.filter;


import com.hall.utils.UserSession;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserSessionFilter extends OncePerRequestFilter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();//请求路径匹配器

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //获取本次请求url
        String requestURI = request.getRequestURI();
        //获取请求参数
        String requestKey = request.getParameter("from");  //请求来自哪里  参数为:"app" 或者 空
        String autoTest = request.getParameter("autoTest");  //app访问web请求的参数 "autoLogin"  全局携带(此参数告诉boke和bokehall过滤器在请求中携带此参数时就执行自动登录)
        String userName = request.getParameter("userName");  //app访问web请求的参数 "当前app客户端中登录的用户名"  全局携带
        String password = request.getParameter("password");  //app访问web请求的参数 "当前app客户端中登录的用户密码" 全局携带
        System.out.println("000000000000000000000000" + requestKey);
        //对于不通过过滤器登录验证但是存在登录后业务的url执行先处理,比如会诊大厅
        if (autoTest != null & requestURI.equals("/bokehall/hall/allHall") & UserSession.getSysUser(request).getId() == null) {//如果请求来自app,并且是会诊大厅,而且未登录,直接自动登录
            if (userName == null || password == null) return;
            response.sendRedirect("/bokehall/user/loginAuto?userName=" + userName + "&password=" + password + "&Historyurl=" + requestURI);
            return;
        }else if(autoTest != null & requestURI.equals("/bokehall/hall/allHall") & UserSession.getSysUser(request).getId() != null){//如果请求来自app,并且是会诊大厅,而且已登录, 但是如果在app端切换了当前登录账户则再次重新登录
            if(!UserSession.getSysUser(request).getLoginName().equals(userName)){
                response.sendRedirect("/bokehall/user/loginAuto?userName=" + userName + "&password=" + password + "&Historyurl=" + requestURI);
                return;
            }
        }
        //定义不需要处理的请求
        String[] urls = new String[]{  //这些请求不走拦截器,直接放行
                "/bokehall/index.jsp",
                "/boke/**",
                "/bokehall/login",
                "/bokehall/user/**",
                "/bokehall/learning/**",
                "/bokeupload/**",         //放行头像图片url
                "/bokehall/resource/**",  //放行所有配置文件
                "/bokehall/static/**",    // 放行所有静态资源文件
                "/bokehall/all/**",
                "/bokehall/appdownload/**",
                "/bokehall/hall/allHall",
                "/bokehall/views/meet/past_meet.html",
                "/bokehall/views/novice/**",
                "/bokehall/my/updateOne",
                "/bokehall/my/uploadImage",
                "/bokehall/files/uploadFromApp",
                "/bokehall/views/novice/teach-index.html",
                "/bokehall/views/info/**"
        };
        System.out.println(">>>>>>>>>>>>请求的url为" + requestURI);
        //判断本次请求是否需要处理
        boolean check = check(urls, requestURI);
        //如果不需要,直接放行
        if (check) {
            System.out.println(">>>>>>>>>>>>>>>>>本次放行了" + requestURI);
            filterChain.doFilter(request, response);
            return;
        }
        //判断登录状态,如果登录直接放行
        if (UserSession.getSysUser(request).getId() != null) {
            //如果app内部重新登录了账户,则重新登录刷新session
            if (!UserSession.getSysUser(request).getLoginName().equals(userName) & "autoLogin".equals(autoTest)) {
                response.sendRedirect("/bokehall/user/loginAuto?userName=" + userName + "&password=" + password + "&Historyurl=" + requestURI);
                return;
            }
            filterChain.doFilter(request, response);
            return;
        }
        //如果是从app发送的请求并且未登录,执行自动登录
        if ("autoLogin".equals(autoTest)) {
            if (userName == null || password == null) return;
            response.sendRedirect("/bokehall/user/loginAuto?userName=" + userName + "&password=" + password + "&Historyurl=" + requestURI);
            System.out.println("拦截到参数" + userName + password + requestURI);
            return;
        }
        //web端如果未登录重定向登录页面(适用于网页访问,例如微信)
        System.out.println(">>>>>>>>>>>>>>>>>>>当前未登录,进行重定向");
        response.sendRedirect("/bokehall/user/login");
        return;
    }

    /**
     * 路径匹配,检查本次请求是否放行
     *
     * @param requestURI   请求的url
     * @return    是否放行
     */
    public boolean check(String[] urls, String requestURI) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI); //路径匹配器
            if (match) {
                return true;
            }
        }
        return false;
    }
}
